﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Converter;
using csla4Skeleton.UI.SL.Resources.Utility;
using csla4Skeleton.UI.SL.DanhMuc.DM_NhaCungCap;
using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.DM_LoaiHoaDon;
using csla4Skeleton.UI.SL.DanhMuc.DM_Kho;
using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.DM_LoaiTienTe;
using csla4Skeleton.UI.SL.DanhMuc.DM_HangHoa;
using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.DM_LoaiMuaSanPham;
using csla4Skeleton.UI.SL.DanhMuc.DM_TaiKhoan;
using csla4Skeleton.UI.SL.DanhMuc.DM_DonViTinh;
using csla4Skeleton.UI.SL.DanhMuc.DM_ThueSuat;
using csla4Skeleton.UI.SL.DanhMuc.DM_TinhChatHangHoa;
using csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.DM_DoiTuongTHCP;
using Accounting.Library;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using System.ComponentModel;
using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.MSP_MuaHangHoa;
using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.MSP_MuaDichVu;
using Utility;
using Accounting.Library.Utility;

namespace csla4Skeleton.UI.SL.NghiepVu.TienMat.TM_PhieuChi
{
    public partial class TM_PhieuChi_MainWindow: UserControl, Csla.Xaml.ISupportNavigation
    {

        #region ISupportNavigation Members
        List<Control> controls = new List<Control>();
        public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            try
            {
                if (parameters == "")
                    Khoa = null;
                else
                    Khoa = Int32.Parse(parameters);
            }
            catch
            {
                Khoa = null;
            }
        }

        public string Title
        {
            get { return "PhieuChi"; }
        }

       
        #endregion

        #region Load Methods
        private System.Nullable<int> Khoa = null;
      
        public TM_PhieuChi_MainWindow(int Khoa)          
        {
            this.Khoa = Khoa;

        }

        public TM_PhieuChi_MainWindow()
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("csla4Skeleton.UI.SL.NghiepVu.TienMat.TM_PhieuChi.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
            
            InitializeComponent();
            ControlBehavior.ControlBehaviors.Navigation(this.TM_PhieuChi_GeneralInformation_GroupBox, controls);
            ControlBehavior.ControlBehaviors.Navigation(this.TM_PhieuChi_MoreInformation_GroupBox, controls);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                
                System.Windows.Data.CollectionViewSource TM_PhieuChi_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TM_PhieuChi_ViewModelSource"];
                if (Khoa == null)
                {
                    TM_PhieuChi_ViewModel TM_PhieuChi_ViewModel = new TM_PhieuChi_ViewModel();
                    TM_PhieuChi_ViewModel.ChildList_AddedNew += new TM_PhieuChi_ViewModel.ChildList_AddedNew_EventHandler(TM_PhieuChi_ViewModel_ChildList_AddedNew);               
                    TM_PhieuChi_ViewModel.TCHangHoaChanged += new EventHandler(TM_PhieuChi_TinhChatHangHoaChanged);
                    TM_PhieuChi_ViewModel.hangHoaIdChanged += new TM_PhieuChi_ViewModel.HangHoaIdChanged_EventHandler(TM_PhieuChi_HangHoaIdChanged); 
                    TM_PhieuChi_ViewModel.LoaiTienTeChanged += new EventHandler(TM_PhieuChi_LoaiTienTeChanged);
                    TM_PhieuChi_ViewModel.ThueSuatIdChanged += new TM_PhieuChi_ViewModel.ThueSuatIdChanged_EventHandler(TM_PhieuChi_ThueSuatIdChanged);
                    TM_PhieuChi_CollectionViewSource.Source = new List<object> { TM_PhieuChi_ViewModel };
                    this.tabControl.Visibility = Visibility.Visible;
                    this.MoreInputGrid.Visibility = Visibility.Visible;

                    this.TM_PhieuChi_PhieuChiChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.TM_PhieuChi_PhieuChiChiTietGridView);
                    this.TM_PhieuChi_ThueChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.TM_PhieuChi_ThueChiTietGridView);

                    ((ControlBehavior.GridViewKeyboardCommandProvider)this.TM_PhieuChi_PhieuChiChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
                    ((ControlBehavior.GridViewKeyboardCommandProvider)this.TM_PhieuChi_ThueChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);

                    ((ControlBehavior.GridViewKeyboardCommandProvider)this.TM_PhieuChi_PhieuChiChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
                    ((ControlBehavior.GridViewKeyboardCommandProvider)this.TM_PhieuChi_ThueChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);

                    
                }
                else
                {
                    TM_PhieuChi_ViewModel TM_PhieuChi_ViewModel = new TM_PhieuChi_ViewModel((int)Khoa);
                    TM_PhieuChi_ViewModel.ChildList_AddedNew += new TM_PhieuChi_ViewModel.ChildList_AddedNew_EventHandler(TM_PhieuChi_ViewModel_ChildList_AddedNew);
                    TM_PhieuChi_ViewModel.TCHangHoaChanged += new EventHandler(TM_PhieuChi_TinhChatHangHoaChanged);
                    TM_PhieuChi_ViewModel.hangHoaIdChanged += new TM_PhieuChi_ViewModel.HangHoaIdChanged_EventHandler(TM_PhieuChi_HangHoaIdChanged);
                    TM_PhieuChi_ViewModel.LoaiTienTeChanged += new EventHandler(TM_PhieuChi_LoaiTienTeChanged);                    
                    TM_PhieuChi_ViewModel.ThueSuatIdChanged += new TM_PhieuChi_ViewModel.ThueSuatIdChanged_EventHandler(TM_PhieuChi_ThueSuatIdChanged);
                    TM_PhieuChi_ViewModel.LoadCompleted += new EventHandler(TM_PhieuChi_ViewModel_LoadCompleted);
                    TM_PhieuChi_CollectionViewSource.Source = new List<object> { TM_PhieuChi_ViewModel };

                    
                }

                System.Windows.Data.CollectionViewSource dm_NhaCungCap_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
                DM_NhaCungCap_ListViewModel dm_NhaCungCap_ListViewModel = new DM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_NhaCungCap);
                dm_NhaCungCap_CollectionViewSource.Source = new List<object> { dm_NhaCungCap_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_LoaiHoaDon_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiHoaDon_ListViewModelSource"];
                DM_LoaiHoaDon_ListViewModel dm_LoaiHoaDon_ListViewModel = new DM_LoaiHoaDon_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_LoaiHoaDon_CollectionViewSource.Source = new List<object> { dm_LoaiHoaDon_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_LoaiTienTe_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiTienTe_ListViewModelSource"];
                DM_LoaiTienTe_ListViewModel dm_LoaiTienTe_ListViewModel = new DM_LoaiTienTe_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_LoaiTienTe_CollectionViewSource.Source = new List<object> { dm_LoaiTienTe_ListViewModel };


                System.Windows.Data.CollectionViewSource dm_DonViTinh_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_DonViTinh_ListViewModelSource"];
                DM_DonViTinh_ListViewModel dm_DonViTinh_ListViewModel = new DM_DonViTinh_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_DonViTinh_CollectionViewSource.Source = new List<object> { dm_DonViTinh_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_ChiTiet_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoanNo_ChiTiet_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoanNo_ChiTiet_CollectionViewSource.Source = new List<object> { dm_TaiKhoanNo_ChiTiet_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TaiKhoanCo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_ChiTiet_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoanCo_ChiTiet_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoanCo_ChiTiet_CollectionViewSource.Source = new List<object> { dm_TaiKhoanCo_ChiTiet_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_Thue_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_Thue_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoanNo_Thue_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoanNo_Thue_CollectionViewSource.Source = new List<object> { dm_TaiKhoanNo_Thue_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TaiKhoanCo_Thue_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_Thue_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoanCo_Thue_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoanCo_Thue_CollectionViewSource.Source = new List<object> { dm_TaiKhoanCo_Thue_ListViewModel };
                
                System.Windows.Data.CollectionViewSource dm_TaiKhoan_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoan_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoan_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoan_CollectionViewSource.Source = new List<object> { dm_TaiKhoan_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_Kho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_Kho_ListViewModelSource"];
                DM_Kho_ListViewModel dm_Kho_ListViewModel = new DM_Kho_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_Kho_CollectionViewSource.Source = new List<object> { dm_Kho_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_LoaiMuaSanPham_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiMuaSanPham_ListViewModelSource"];
                DM_LoaiMuaSanPham_ListViewModel dm_LoaiMuaSanPham_ListViewModel = new DM_LoaiMuaSanPham_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_LoaiMuaSanPham_CollectionViewSource.Source = new List<object> { dm_LoaiMuaSanPham_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_DoiTuongTHCP_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_DoiTuongTHCP_ListViewModelSource"];
                DM_DoiTuongTHCP_ListViewModel dm_DoiTuongTHCP_ListViewModel = new DM_DoiTuongTHCP_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_DoiTuongTHCP_CollectionViewSource.Source = new List<object> { dm_DoiTuongTHCP_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_ThueSuat_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_ThueSuat_ListViewModelSource"];
                DM_ThueSuat_ListViewModel dm_ThueSuat_ListViewModel = new DM_ThueSuat_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_ThueSuat_CollectionViewSource.Source = new List<object> { dm_ThueSuat_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TinhChatHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TinhChatHangHoa_ListViewModelSource"];
                DM_TinhChatHangHoa_ListViewModel dm_TinhChatHangHoa_ListViewModel = new DM_TinhChatHangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TinhChatHangHoa_CollectionViewSource.Source = new List<object> { dm_TinhChatHangHoa_ListViewModel };

                DoFilterOnDefaultDM_SanPham();

            }
        }

        #endregion
       

        #region Event Handle
        private void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                TM_PhieuChi_ViewModel vm = GetViewModel();
                if (e.Key == Key.S)
                {
                    if (vm.CanSave)
                    {
                        vm.Save(sender, new Csla.Xaml.ExecuteEventArgs());
                    }
                }
                else if (e.Key == Key.F6)
                {
                    AddItems();
                }
                else if (e.Key == Key.D1)
                {
                    this.TM_PhieuChi_PhieuChiChiTietTab.IsSelected = true;
                }
                else if (e.Key == Key.D2)
                {
                    this.TM_PhieuChi_ThueChiTietTab.IsSelected = true;
                }
               
                
            }
            e.Handled = true;
        }

        void AddItemButton_Click(object sender, RoutedEventArgs e)
        {

            AddItems();
        }

        void DeleteItemButton_Click(object sender, RoutedEventArgs e)
        {
            DeleteItems();
          
        }

        public void AddItems()
        {
            if (this.tabControl.Visibility == Visibility.Visible)
            {
                if (this.TM_PhieuChi_PhieuChiChiTietTab.IsSelected)
                {
                    AddPhieuChiDetailsItem();
                }
            }
            else if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
            {
                if (this.MSP_MuaHangHoa_MuaHangHoaChiTietTab.IsSelected)
                {
                    AddMuaHangHoaDetailsItem();
                }
            }
            else if (this.MuaDichVuTabControl.Visibility == Visibility.Visible)
            {
                if (this.MSP_MuaDichVu_MuaDichVuChiTietTab.IsSelected)
                {
                    AddMuaDichVuDetailsItem();
                }
            }
            
        }
        private RadGridView GetActiveGridView()
        {
            if (this.TM_PhieuChi_PhieuChiChiTietTab.IsSelected)
            {
                return TM_PhieuChi_PhieuChiChiTietGridView;
            }
            else if (this.TM_PhieuChi_ThueChiTietTab.IsSelected)
            {
                return TM_PhieuChi_ThueChiTietGridView;
            }
            else if (this.MSP_MuaHangHoa_MuaHangHoaChiTietTab.IsSelected)
            {
                return MSP_MuaHangHoa_MuaHangHoaChiTietGridView;
            }
            else if (this.MSP_MuaHangHoa_ThueChiTietTab.IsSelected)
            {
                return MSP_MuaHangHoa_ThueChiTietGridView;
            }
            else if (this.MSP_MuaHangHoa_ChiPhiMuaHangTab.IsSelected)
            {
                return MSP_MuaHangHoa_ChiPhiMuaHangGridView;
            }
            else if (this.MSP_MuaHangHoa_TSThieuCXLTab.IsSelected)
            {
                return MSP_MuaHangHoa_TSThieuCXLGridView;
            }
            else if (this.MSP_MuaHangHoa_TSThuaCXLTab.IsSelected)
            {
                return MSP_MuaHangHoa_TSThuaCXLGridView;
            }
            else if (this.MSP_MuaHangHoa_ThueNKTab.IsSelected)
            {
                return MSP_MuaHangHoa_ThueNKGridView;
            }
            else if (this.MSP_MuaHangHoa_ThueTTDBTab.IsSelected)
            {
                return MSP_MuaHangHoa_ThueTTDBGridView;
            }
            else if (this.MSP_MuaDichVu_MuaDichVuChiTietTab.IsSelected)
            {
                return MSP_MuaDichVu_MuaDichVuChiTietGridView;
            }
            else if (this.MSP_MuaDichVu_ThueChiTietTab.IsSelected)
            {
                return MSP_MuaDichVu_ThueChiTietGridView;
            }
            else if (this.MSP_MuaDichVu_ChiPhiMuaDichVuTab.IsSelected)
            {
                return MSP_MuaDichVu_ChiPhiMuaDichVuGridView;
            }
           
            
            return null;
        }



        void DeleteItems()
        {
            if (this.tabControl.Visibility == Visibility.Visible)
            {
               
                if (this.TM_PhieuChi_PhieuChiChiTietTab.IsSelected)
                {
                    TM_PhieuChi_ChiTietChild ChiTietChild = (TM_PhieuChi_ChiTietChild)(this.TM_PhieuChi_PhieuChiChiTietGridView.SelectedItem);
                    IList<TM_PhieuChi_ChiTietChild> list = this.GetViewModel().Model.TM_PhieuChi_ChiTiets;
                    int index = list.IndexOf(ChiTietChild);
                    DeletePhieuChiDetailsItem(index);
                }
            }
            else if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
            {
                if (this.MSP_MuaHangHoa_MuaHangHoaChiTietTab.IsSelected)
                {
                    MSP_MuaHangHoa_ChiTietChild ChiTietChild = (MSP_MuaHangHoa_ChiTietChild)(this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.SelectedItem);
                    IList<MSP_MuaHangHoa_ChiTietChild> list = this.GetMuaHangHoaViewModel().Model.MSP_MuaHangHoa_ChiTiets;
                    int index = list.IndexOf(ChiTietChild);
                    DeleteMuaHangHoaDetailsItem(index);
                }
            }
            else if (this.MuaDichVuTabControl.Visibility == Visibility.Visible)
            {
                if (this.MSP_MuaDichVu_MuaDichVuChiTietTab.IsSelected)
                {
                    MSP_MuaDichVu_ChiTietChild ChiTietChild = (MSP_MuaDichVu_ChiTietChild)(this.MSP_MuaDichVu_MuaDichVuChiTietGridView.SelectedItem);
                    IList<MSP_MuaDichVu_ChiTietChild> list = this.GetMuaDichVuViewModel().Model.MSP_MuaDichVu_ChiTiets;
                    int index = list.IndexOf(ChiTietChild);
                    DeleteMuaDichVuDetailsItem(index);
                }
            }
           
            
        }
           
        void GridView_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
        {
            var grid = e.OwnerGridViewItemsControl;
            if (grid.Items.Count > 0)
            {
                GridViewUtil.FocusRow((RadGridView)grid, grid.Items.Count - 1);
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[grid.Items.Count - 1], grid.Columns[0]);
                    grid.Focus();
                    grid.BeginEdit();
                }));
            }
        }
              
        private void SaveAndNewButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.tabControl.Visibility == Visibility.Visible)
            {
                this.GetViewModel().SaveAndNew();
            }
            else if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
            {
                this.GetMuaHangHoaViewModel().SaveAndNew();
            }
            else if (this.MuaDichVuTabControl.Visibility == Visibility.Visible)
            {
                this.GetMuaDichVuViewModel().SaveAndNew();
            }
        }

        private void DatePicker_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {

                    if (this.tabControl.Visibility == Visibility.Visible)
                    {
                        this.AddPhieuChiDetailsItem();
                    }
                    else if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
                    {

                        this.AddMuaHangHoaDetailsItem();
                    }
                    else if (this.MuaDichVuTabControl.Visibility == Visibility.Visible)
                    {
                        this.AddMuaDichVuDetailsItem();
                    }
                }
            }
            else
            {
                ControlBehavior.DatePickerBehaviors.DatePicker_KeyUp(sender, e, controls);
            }
        }

        private void ComboBox_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    if (this.tabControl.Visibility == Visibility.Visible)
                    {
                        this.AddPhieuChiDetailsItem();
                    }
                    else if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
                    {

                        this.AddMuaHangHoaDetailsItem();
                    }
                    else if (this.MuaDichVuTabControl.Visibility == Visibility.Visible)
                    {
                        this.AddMuaDichVuDetailsItem();
                    }
                }
            }
            else
            {
                ControlBehavior.ComboBoxBehaviors.ComboBox_KeyUp(sender, e, controls);
            }
        }

        private void Control_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    if (this.tabControl.Visibility == Visibility.Visible)
                    {
                        this.AddPhieuChiDetailsItem();
                    }
                    else if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
                    {
                       
                        this.AddMuaHangHoaDetailsItem();
                    }
                    else if (this.MuaDichVuTabControl.Visibility == Visibility.Visible)
                    {
                        this.AddMuaDichVuDetailsItem();
                    }
                    
                }
            }
            else
            {
                ControlBehavior.ControlBehaviors.Control_KeyUp(sender, e, controls);
            }
        }


        private void BookedItemButton_Click(object sender, RoutedEventArgs e)
        {
            GetViewModel().GhiSo();
        }

        private void DeleteBookedItemButton_Click(object sender, RoutedEventArgs e)
        {
            GetViewModel().BoGhi();
        }

        void OnTabKeyPress(object sender, EventArgs e)
        {
            if (this.tabControl.Visibility == Visibility.Visible)
            {
                this.tabControl.SelectedItem = tabControl.Items[0];
                this.AddPhieuChiDetailsItem();
            }
            else if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
            {
                this.MuaHangHoaTabControl.SelectedItem = MuaHangHoaTabControl.Items[0];
                this.AddMuaHangHoaDetailsItem();
            }
            else if (this.MuaDichVuTabControl.Visibility == Visibility.Visible)
            {
                this.MuaDichVuTabControl.SelectedItem = MuaDichVuTabControl.Items[0];
                this.AddMuaDichVuDetailsItem();
            }

        }

        void OnReachLastColumn(object sender, ReachLastColumnArgs e)
        {
            if (tabControl.Visibility == Visibility.Visible)
            {
                List<TabItem> AvailableTabItem = new List<TabItem>();
                int indexCurrentTab = 0;
                foreach (TabItem item in this.tabControl.Items)
                {
                    if (item.Visibility == System.Windows.Visibility.Visible)
                    {
                        AvailableTabItem.Add(item);
                        if (item.GetHashCode() == tabControl.SelectedItem.GetHashCode())
                        {
                            indexCurrentTab = AvailableTabItem.IndexOf(item);
                        }

                    }
                }


                TabItem[] list = AvailableTabItem.ToArray();
                if (indexCurrentTab == (AvailableTabItem.Count - 1))
                {
                    this.tabControl.SelectedItem = list[0];
                    FocusAfterChangeTab(this.TM_PhieuChi_PhieuChiChiTietGridView, e.Index);

                }
                else
                {
                    this.tabControl.SelectedItem = list[indexCurrentTab + 1];
                    if (list[indexCurrentTab + 1].Name.Equals("TM_PhieuChi_ThueChiTietTab"))
                    {
                        FocusAfterChangeTab(this.TM_PhieuChi_ThueChiTietGridView, e.Index);
                    }
                }
            }
            else if (MuaHangHoaTabControl.Visibility == Visibility.Visible)
            {
                List<TabItem> AvailableTabItem = new List<TabItem>();
                int indexCurrentTab = 0;
                foreach (TabItem item in this.MuaHangHoaTabControl.Items)
                {
                    if (item.Visibility == System.Windows.Visibility.Visible)
                    {
                        AvailableTabItem.Add(item);
                        if (item.GetHashCode() == MuaHangHoaTabControl.SelectedItem.GetHashCode())
                        {
                            indexCurrentTab = AvailableTabItem.IndexOf(item);
                        }

                    }
                }


                TabItem[] list = AvailableTabItem.ToArray();
                if (indexCurrentTab == (AvailableTabItem.Count - 1))
                {
                    this.MuaHangHoaTabControl.SelectedItem = list[0];
                    FocusAfterChangeTab(this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView, e.Index);

                }
                else
                {
                    this.MuaHangHoaTabControl.SelectedItem = list[indexCurrentTab + 1];
                    if (list[indexCurrentTab + 1].Name.Equals("MSP_MuaHangHoa_ChiPhiMuaHangTab"))
                    {
                        FocusAfterChangeTab(this.MSP_MuaHangHoa_ChiPhiMuaHangGridView, e.Index);
                    }
                    else if (list[indexCurrentTab + 1].Name.Equals("MSP_MuaHangHoa_ThueChiTietTab"))
                    {
                        FocusAfterChangeTab(this.MSP_MuaHangHoa_ThueChiTietGridView, e.Index);

                    }
                    else if (list[indexCurrentTab + 1].Name.Equals("MSP_MuaHangHoa_ThueNKTab"))
                    {
                        FocusAfterChangeTab(this.MSP_MuaHangHoa_ThueNKGridView, e.Index);

                    }
                    else if (list[indexCurrentTab + 1].Name.Equals("MSP_MuaHangHoa_ThueTTDBTab"))
                    {
                        FocusAfterChangeTab(this.MSP_MuaHangHoa_ThueTTDBGridView, e.Index);

                    }
                    else if (list[indexCurrentTab + 1].Name.Equals("MSP_MuaHangHoa_TSThieuCXLTab"))
                    {
                        FocusAfterChangeTab(this.MSP_MuaHangHoa_TSThieuCXLGridView, e.Index);

                    }
                    else if (list[indexCurrentTab + 1].Name.Equals("MSP_MuaHangHoa_TSThuaCXLTab"))
                    {
                        FocusAfterChangeTab(this.MSP_MuaHangHoa_TSThuaCXLGridView, e.Index);

                    }
                }
            }
            else if (MuaDichVuTabControl.Visibility == Visibility.Visible)
            {
                List<TabItem> AvailableTabItem = new List<TabItem>();
                int indexCurrentTab = 0;
                foreach (TabItem item in this.MuaDichVuTabControl.Items)
                {
                    if (item.Visibility == System.Windows.Visibility.Visible)
                    {
                        AvailableTabItem.Add(item);
                        if (item.GetHashCode() == MuaDichVuTabControl.SelectedItem.GetHashCode())
                        {
                            indexCurrentTab = AvailableTabItem.IndexOf(item);
                        }

                    }
                }


                TabItem[] list = AvailableTabItem.ToArray();
                if (indexCurrentTab == (AvailableTabItem.Count - 1))
                {
                    this.MuaDichVuTabControl.SelectedItem = list[0];
                    FocusAfterChangeTab(this.MSP_MuaDichVu_MuaDichVuChiTietGridView, e.Index);
                }
                else
                {
                    this.MuaDichVuTabControl.SelectedItem = list[indexCurrentTab + 1];
                    if (list[indexCurrentTab + 1].Name.Equals("MSP_MuaDichVu_ChiPhiMuaDichVuTab"))
                    {
                        GridViewUtil.FocusRow(this.MSP_MuaDichVu_ChiPhiMuaDichVuGridView, e.Index);
                        FocusAfterChangeTab(this.MSP_MuaDichVu_ChiPhiMuaDichVuGridView, e.Index);
                    }
                    else if (list[indexCurrentTab + 1].Name.Equals("MSP_MuaDichVu_ThueChiTietTab"))
                    {
                        FocusAfterChangeTab(this.MSP_MuaDichVu_ThueChiTietGridView, e.Index);

                    }


                }
            }

        }

        private void FocusAfterChangeTab(GridViewDataControl grid, int index)
        {
             
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[index], grid.Columns[0]);
                    grid.Focus();
                    grid.BeginEdit();
                }));
           
        }
        private void TM_PhieuChi_GeneralInformation_NhaCungCapId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_NhaCungCap_ChildWindow dm_NhaCungCap_ChildWindow = new DM_NhaCungCap_ChildWindow();
            dm_NhaCungCap_ChildWindow.Show();
            dm_NhaCungCap_ChildWindow.SaveClicked += new EventHandler(dm_NhaCungCap_ViewModel_Saved);
        }

        

        private void TM_PhieuChi_MoreInformation_LoaiTienId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_LoaiTienTe_ChildWindow dm_LoaiTienTe_ChildWindow = new DM_LoaiTienTe_ChildWindow();
            dm_LoaiTienTe_ChildWindow.Show();
            dm_LoaiTienTe_ChildWindow.SaveClicked += new EventHandler(dm_LoaiTienTe_ViewModel_Saved);
        }

       
        #endregion

        #region View Model Related Methods
        private TM_PhieuChi_ViewModel GetViewModel()
        {
            System.Windows.Data.CollectionViewSource TM_PhieuChi_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TM_PhieuChi_ViewModelSource"];
            return ((List<object>)TM_PhieuChi_CollectionViewSource.Source)[0] as TM_PhieuChi_ViewModel;
        }

        private MSP_MuaHangHoa_ViewModel GetMuaHangHoaViewModel()
        {
            System.Windows.Data.CollectionViewSource MSP_MuaHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaHangHoa_ViewModelSource"];
            return ((List<object>)MSP_MuaHangHoa_CollectionViewSource.Source)[0] as MSP_MuaHangHoa_ViewModel;
        }

        private MSP_MuaDichVu_ViewModel GetMuaDichVuViewModel()
        {
            System.Windows.Data.CollectionViewSource MSP_MuaDichVu_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaDichVu_ViewModelSource"];
            return ((List<object>)MSP_MuaDichVu_CollectionViewSource.Source)[0] as MSP_MuaDichVu_ViewModel;
        }

        public void TM_PhieuChi_ViewModel_LoadCompleted(object sender, EventArgs e)
        {
            if(this.GetViewModel().Model.ChungTuThayTheId == null)
            {
                this.tabControl.Visibility = Visibility.Visible;
                this.MoreInputGrid.Visibility = Visibility.Visible;

                this.GetViewModel().Model.TongTien = this.GetViewModel().Model.TongTienQĐ / this.GetViewModel().Model.TyGia;
                this.GetViewModel().Model.TienThueGTGT = this.GetViewModel().Model.TienThueGTGTQĐ / this.GetViewModel().Model.TyGia;
                this.GetViewModel().Model.TongTienThanhToan = this.GetViewModel().Model.TongTienThanhToanQĐ / this.GetViewModel().Model.TyGia;

                this.TM_PhieuChi_PhieuChiChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.TM_PhieuChi_PhieuChiChiTietGridView);
                this.TM_PhieuChi_ThueChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.TM_PhieuChi_ThueChiTietGridView);

                ((ControlBehavior.GridViewKeyboardCommandProvider)this.TM_PhieuChi_PhieuChiChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.TM_PhieuChi_ThueChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);

                ((ControlBehavior.GridViewKeyboardCommandProvider)this.TM_PhieuChi_PhieuChiChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.TM_PhieuChi_ThueChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);

            }
            else if( this.GetViewModel().Model.LoaiChungTuThayTheId == 301)
            {
                this.MuaHangHoaTabControl.Visibility = Visibility.Visible;

                System.Windows.Data.CollectionViewSource msp_MuaHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaHangHoa_ViewModelSource"];
                MSP_MuaHangHoa_ViewModel msp_MuaHangHoa_ViewModel = new MSP_MuaHangHoa_ViewModel((int)this.GetViewModel().Model.ChungTuThayTheId);
                msp_MuaHangHoa_ViewModel.ChildList_AddedNew += new MSP_MuaHangHoa_ViewModel.ChildList_AddedNew_EventHandler(msp_MuaHangHoa_ViewModel_ChildList_AddedNew);
                msp_MuaHangHoa_ViewModel.LoadCompleted += new EventHandler(msp_MuaHangHoa_ViewModel_LoadCompleted);
                msp_MuaHangHoa_ViewModel.hangHoaIdChanged += new MSP_MuaHangHoa_ViewModel.HangHoaIdChanged_EventHandler(msp_MuaHangHoa_HangHoaIdChanged);
                msp_MuaHangHoa_ViewModel.ThueSuatIdChanged += new MSP_MuaHangHoa_ViewModel.ThueSuatIdChanged_EventHandler(msp_MuaHangHoa_ThueSuatIdChanged);
                msp_MuaHangHoa_ViewModel.TongTienThanhToanChanged += new EventHandler(OnTongTienThanhToanChanged);
                msp_MuaHangHoa_ViewModel.TongTienHangChanged += new EventHandler(OnTongTienHangChanged);
                msp_MuaHangHoa_ViewModel.TienThueGTGTChanged += new EventHandler(OnTienThueGTGTChanged);
                msp_MuaHangHoa_CollectionViewSource.Source = new List<object> { msp_MuaHangHoa_ViewModel };

                this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView);
                this.MSP_MuaHangHoa_ThueChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.MSP_MuaHangHoa_ThueChiTietGridView);
                this.MSP_MuaHangHoa_ChiPhiMuaHangGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.MSP_MuaHangHoa_ChiPhiMuaHangGridView);
                this.MSP_MuaHangHoa_TSThieuCXLGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.MSP_MuaHangHoa_TSThieuCXLGridView);
                this.MSP_MuaHangHoa_TSThuaCXLGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.MSP_MuaHangHoa_TSThuaCXLGridView);
                this.MSP_MuaHangHoa_ThueNKGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.MSP_MuaHangHoa_ThueNKGridView);
                this.MSP_MuaHangHoa_ThueTTDBGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.MSP_MuaHangHoa_ThueTTDBGridView);

                ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_ThueChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_ChiPhiMuaHangGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_TSThieuCXLGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_TSThuaCXLGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_ThueNKGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_ThueTTDBGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);

                ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler (OnTabKeyPress);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_ThueChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_ChiPhiMuaHangGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_TSThieuCXLGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_TSThuaCXLGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_ThueNKGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_ThueTTDBGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
            }
            else if (this.GetViewModel().Model.LoaiChungTuThayTheId == 302)
            {
                this.MuaDichVuTabControl.Visibility = Visibility.Visible;

                System.Windows.Data.CollectionViewSource msp_MuaDichVu_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaDichVu_ViewModelSource"];
                MSP_MuaDichVu_ViewModel msp_MuaDichVu_ViewModel = new MSP_MuaDichVu_ViewModel((int)this.GetViewModel().Model.ChungTuThayTheId);
                msp_MuaDichVu_ViewModel.ChildList_AddedNew += new MSP_MuaDichVu_ViewModel.ChildList_AddedNew_EventHandler(MSP_MuaDichVu_ViewModel_ChildList_AddedNew);
                msp_MuaDichVu_ViewModel.LoadCompleteted += new EventHandler(msp_MuaDichVu_ViewModel_LoadCompleteted);
                msp_MuaDichVu_ViewModel.ThueSuatIdChanged += new MSP_MuaDichVu_ViewModel.ThueSuatIdChanged_EventHandler(msp_MuaDichVu_ThueSuatIdChanged);
                msp_MuaDichVu_ViewModel.hangHoaIdChanged += new MSP_MuaDichVu_ViewModel.HangHoaIdChanged_EventHandler(msp_MuaDichVu_HangHoaIdChanged);
                msp_MuaDichVu_ViewModel.TongTienThanhToanChanged += new EventHandler(OnTongTienThanhToanChanged);
                msp_MuaDichVu_ViewModel.TongTienHangChanged += new EventHandler(OnTongTienHangChanged);
                msp_MuaDichVu_ViewModel.TienThueGTGTChanged += new EventHandler(OnTienThueGTGTChanged);
                msp_MuaDichVu_CollectionViewSource.Source = new List<object> { msp_MuaDichVu_ViewModel };

                this.MSP_MuaDichVu_MuaDichVuChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.MSP_MuaDichVu_MuaDichVuChiTietGridView);
                this.MSP_MuaDichVu_ThueChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.MSP_MuaDichVu_ThueChiTietGridView);
                this.MSP_MuaDichVu_ChiPhiMuaDichVuGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.MSP_MuaDichVu_ChiPhiMuaDichVuGridView);

                ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaDichVu_MuaDichVuChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaDichVu_ThueChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaDichVu_ChiPhiMuaDichVuGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);

                ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaDichVu_MuaDichVuChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaDichVu_ThueChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaDichVu_ChiPhiMuaDichVuGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
            }
        }

        private void OnTongTienThanhToanChanged(object sender, EventArgs e)
        {
            if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
            {
                this.GetViewModel().Model.TongTienThanhToan = this.GetMuaHangHoaViewModel().Model.TongTienThanhToan;
                this.GetViewModel().Model.TongTienThanhToanQĐ = this.GetMuaHangHoaViewModel().Model.TongTienThanhToan * GetMuaHangHoaViewModel().Model.TyGia;
            }
            else if (this.MuaDichVuTabControl.Visibility == Visibility.Visible)
            {
                this.GetViewModel().Model.TongTienThanhToan = this.GetMuaDichVuViewModel().Model.TongTienThanhToan;
                this.GetViewModel().Model.TongTienThanhToanQĐ = this.GetMuaDichVuViewModel().Model.TongTienThanhToan * GetMuaDichVuViewModel().Model.TyGia;
            }
        }

        private void OnTongTienHangChanged(object sender, EventArgs e)
        {
            if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
            {
                this.GetViewModel().Model.TongTien = this.GetMuaHangHoaViewModel().Model.TongTienHang;
                this.GetViewModel().Model.TongTienQĐ = this.GetMuaHangHoaViewModel().Model.TongTienHang * GetMuaHangHoaViewModel().Model.TyGia;
            }
            else if (this.MuaDichVuTabControl.Visibility == Visibility.Visible)
            {
                this.GetViewModel().Model.TongTien = this.GetMuaDichVuViewModel().Model.TongTienHang;
                this.GetViewModel().Model.TongTienQĐ = this.GetMuaDichVuViewModel().Model.TongTienHang * GetMuaDichVuViewModel().Model.TyGia;
            }
        }

        private void OnTienThueGTGTChanged(object sender, EventArgs e)
        {
            if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
            {
                this.GetViewModel().Model.TienThueGTGT = this.GetMuaHangHoaViewModel().Model.TienThueGTGT;
                this.GetViewModel().Model.TienThueGTGTQĐ = this.GetMuaHangHoaViewModel().Model.TienThueGTGT * GetMuaHangHoaViewModel().Model.TyGia;
            }
            else if (this.MuaDichVuTabControl.Visibility == Visibility.Visible)
            {
                this.GetViewModel().Model.TienThueGTGT = this.GetMuaDichVuViewModel().Model.TienThueGTGT;
                this.GetViewModel().Model.TienThueGTGTQĐ = this.GetMuaDichVuViewModel().Model.TienThueGTGT * GetMuaDichVuViewModel().Model.TyGia;
            }
        }
        private void msp_MuaHangHoa_ViewModel_LoadCompleted(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource msp_MuaHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaHangHoa_ViewModelSource"];
            MSP_MuaHangHoa_ViewModel muaHangHoaViewModel = ((List<object>)msp_MuaHangHoa_CollectionViewSource.Source)[0] as MSP_MuaHangHoa_ViewModel;

            this.GetViewModel().Model.TongTien = this.GetViewModel().Model.TongTienQĐ / muaHangHoaViewModel.Model.TyGia;
            this.GetViewModel().Model.TienThueGTGT = this.GetViewModel().Model.TienThueGTGTQĐ / muaHangHoaViewModel.Model.TyGia;
            this.GetViewModel().Model.TongTienThanhToan = this.GetViewModel().Model.TongTienThanhToanQĐ / muaHangHoaViewModel.Model.TyGia;

            if (muaHangHoaViewModel.Model.LoaiMuaSanPhamId == 1)
            {
                this.MSP_MuaHangHoa_ThueNKTab.Visibility = Visibility.Collapsed;
                this.MSP_MuaHangHoa_ThueTTDBTab.Visibility = Visibility.Collapsed;
            }
            if (muaHangHoaViewModel.Model.LoaiTienId == 3)
            {
                this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.Columns[8].IsVisible = false;
                this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.Columns[10].IsVisible = false;
                this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.Columns[13].IsVisible = false;
                this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.Columns[15].IsVisible = false;
                this.MSP_MuaHangHoa_ThueNKGridView.Columns[4].IsVisible = false;
                this.MSP_MuaHangHoa_ThueTTDBGridView.Columns[4].IsVisible = false;
                this.MSP_MuaHangHoa_ThueChiTietGridView.Columns[4].IsVisible = false;
                this.MSP_MuaHangHoa_TSThieuCXLGridView.Columns[3].IsVisible = false;
                this.MSP_MuaHangHoa_TSThuaCXLGridView.Columns[3].IsVisible = false;
                this.MSP_MuaHangHoa_ChiPhiMuaHangGridView.Columns[3].IsVisible = false;
            }
        }
        private void msp_MuaDichVu_ViewModel_LoadCompleteted(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource msp_MuaDichVu_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaDichVu_ViewModelSource"];
            MSP_MuaDichVu_ViewModel muaDichVuViewModel = ((List<object>)msp_MuaDichVu_CollectionViewSource.Source)[0] as MSP_MuaDichVu_ViewModel;

            this.GetViewModel().Model.TongTien = this.GetViewModel().Model.TongTienQĐ / muaDichVuViewModel.Model.TyGia;
            this.GetViewModel().Model.TienThueGTGT = this.GetViewModel().Model.TienThueGTGTQĐ / muaDichVuViewModel.Model.TyGia;
            this.GetViewModel().Model.TongTienThanhToan = this.GetViewModel().Model.TongTienThanhToanQĐ / muaDichVuViewModel.Model.TyGia;

            if (muaDichVuViewModel.Model.LoaiTienId == 3)
            {
                this.MSP_MuaDichVu_MuaDichVuChiTietGridView.Columns[6].IsVisible = false;
                this.MSP_MuaDichVu_MuaDichVuChiTietGridView.Columns[8].IsVisible = false;
                this.MSP_MuaDichVu_MuaDichVuChiTietGridView.Columns[11].IsVisible = false;
                this.MSP_MuaDichVu_ThueChiTietGridView.Columns[3].IsVisible = false;
                this.MSP_MuaDichVu_ChiPhiMuaDichVuGridView.Columns[2].IsVisible = false;

            }
        }

        void msp_MuaHangHoa_ThueSuatIdChanged(object sender, ThueSuatIdChangedEventArgs e)
        {

            System.Windows.Data.CollectionViewSource msp_MuaHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaHangHoa_ViewModelSource"];
            MSP_MuaHangHoa_ViewModel muaHangHoaViewModel = ((List<object>)msp_MuaHangHoa_CollectionViewSource.Source)[0] as MSP_MuaHangHoa_ViewModel;

            System.Windows.Data.CollectionViewSource dm_ThueSuat_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_ThueSuat_ListViewModelSource"];
            DM_ThueSuat_ListViewModel dm_ThueSuat_ListViewModel = ((List<object>)dm_ThueSuat_CollectionViewSource.Source)[0] as DM_ThueSuat_ListViewModel;

            MSP_MuaHangHoa_ThueChild ThueChild = muaHangHoaViewModel.Model.MSP_MuaHangHoa_Thues[e.Index];
            IList<DM_ThueSuatInfo> list = dm_ThueSuat_ListViewModel.Model.getListOfInfo();
            foreach (DM_ThueSuatInfo item in list)
            {
                if (item.Khoa == e.ThueSuatId)
                {
                    ThueChild.ThueSuatValue = item.GiaTriTinhThue;
                    break;
                }
            }
        }

        void msp_MuaDichVu_ThueSuatIdChanged(object sender, ThueSuatIdChangedEventArgs e)
        {
            System.Windows.Data.CollectionViewSource msp_MuaDichVu_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaDichVu_ViewModelSource"];
            MSP_MuaDichVu_ViewModel muaDichVuViewModel = ((List<object>)msp_MuaDichVu_CollectionViewSource.Source)[0] as MSP_MuaDichVu_ViewModel;

            System.Windows.Data.CollectionViewSource dm_ThueSuat_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_ThueSuat_ListViewModelSource"];
            DM_ThueSuat_ListViewModel dm_ThueSuat_ListViewModel = ((List<object>)dm_ThueSuat_CollectionViewSource.Source)[0] as DM_ThueSuat_ListViewModel;

            MSP_MuaDichVu_ThueChild ThueChild = muaDichVuViewModel.Model.MSP_MuaDichVu_Thues[e.Index];
            IList<DM_ThueSuatInfo> list = dm_ThueSuat_ListViewModel.Model.getListOfInfo();
            foreach (DM_ThueSuatInfo item in list)
            {
                if (item.Khoa == e.ThueSuatId)
                {
                    ThueChild.ThueSuatValue = item.GiaTriTinhThue;
                    break;
                }
            }


        }
        void dm_NhaCungCap_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_NhaCungCap_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
            DM_NhaCungCap_ListViewModel vm = ((List<object>)dm_NhaCungCap_ListViewSource.Source)[0] as DM_NhaCungCap_ListViewModel;
            vm.GetDM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_NhaCungCap);
        }

        void dm_LoaiTienTe_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_LoaiTienTe_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiTienTe_ListViewModelSource"];
            DM_LoaiTienTe_ListViewModel vm = ((List<object>)dm_LoaiTienTe_ListViewSource.Source)[0] as DM_LoaiTienTe_ListViewModel;
            vm.GetDM_LoaiTienTe_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        public void DoFilterOnDefaultDM_SanPham()
        {
           
            System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
            DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = new DM_HangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
            dm_HangHoa_CollectionViewSource.Source = new List<object> { dm_HangHoa_ListViewModel };
           
        }
     

        void TM_PhieuChi_ThueSuatIdChanged(object sender, ThueSuatIdChangedEventArgs e)
        {
            
            System.Windows.Data.CollectionViewSource dm_ThueSuat_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_ThueSuat_ListViewModelSource"];
            DM_ThueSuat_ListViewModel dm_ThueSuat_ListViewModel = ((List<object>)dm_ThueSuat_CollectionViewSource.Source)[0] as DM_ThueSuat_ListViewModel;    
            
            TM_PhieuChi_ThueChild ThueChild = GetViewModel().Model.TM_PhieuChi_Thues[e.Index];
            IList<DM_ThueSuatInfo> list = dm_ThueSuat_ListViewModel.Model.getListOfInfo();
            foreach (DM_ThueSuatInfo item in list)
            {
                if (item.Khoa == e.ThueSuatId)
                {
                    ThueChild.ThueSuatValue = (double) item.GiaTriTinhThue;
                }
            }

           

        }
        void TM_PhieuChi_TinhChatHangHoaChanged(object sender, EventArgs e)
        {
            int? tinhChatHangHoaId = this.GetViewModel().Model.TinhChatHangHoaId;
            
            System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
            DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = ((List<object>)dm_HangHoa_CollectionViewSource.Source)[0] as DM_HangHoa_ListViewModel;
            dm_HangHoa_ListViewModel.GetDM_HangHoa((int)tinhChatHangHoaId);
              
           
        }

        void TM_PhieuChi_LoaiTienTeChanged(object sender, EventArgs e)
        {
            
            System.Windows.Data.CollectionViewSource dm_LoaiTienTe_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiTienTe_ListViewModelSource"];
            DM_LoaiTienTe_ListViewModel dm_LoaiTienTe_ListViewModel = ((List<object>)dm_LoaiTienTe_CollectionViewSource.Source)[0] as DM_LoaiTienTe_ListViewModel;
            IList<DM_LoaiTienTeInfo> list = dm_LoaiTienTe_ListViewModel.Model.getListOfInfo();
            foreach( DM_LoaiTienTeInfo item in list)
            {
                if (item.Khoa == GetViewModel().Model.LoaiTienId)
                {
                    GetViewModel().Model.TyGia = item.TyGiaQuyDoi;
                    break;
                }
            }
        }
        void msp_MuaDichVu_HangHoaIdChanged(object sender, HangHoaIdChangedEventArgs e)
        {
        }
        void msp_MuaHangHoa_HangHoaIdChanged(object sender, HangHoaIdChangedEventArgs e)
        {
            System.Windows.Data.CollectionViewSource msp_MuaHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaHangHoa_ViewModelSource"];
            MSP_MuaHangHoa_ViewModel muaHangHoaViewModel = ((List<object>)msp_MuaHangHoa_CollectionViewSource.Source)[0] as MSP_MuaHangHoa_ViewModel;

            MSP_MuaHangHoa_ChiTietChild ChiTietChild = muaHangHoaViewModel.Model.MSP_MuaHangHoa_ChiTiets[e.Index];
            MSP_MuaHangHoa_ThueChild ThueChild = muaHangHoaViewModel.Model.MSP_MuaHangHoa_Thues[e.Index];
            MSP_MuaHangHoa_ChiPhiMuaHangChild ChiPhiMuaHangChild = muaHangHoaViewModel.Model.MSP_MuaHangHoa_ChiPhiMuaHangs[e.Index];
            MSP_MuaHangHoa_TSThuaCXLChild TSThuaCXLChild = muaHangHoaViewModel.Model.MSP_MuaHangHoa_TSThuaCXLs[e.Index];
            MSP_MuaHangHoa_TSThieuCXLChild TSThieuCXLChild = muaHangHoaViewModel.Model.MSP_MuaHangHoa_TSThieuCXLs[e.Index];
            MSP_MuaHangHoa_ThueNKChild ThueNKChild = muaHangHoaViewModel.Model.MSP_MuaHangHoa_ThueNKs[e.Index];
            MSP_MuaHangHoa_ThueTTDBChild ThueTTDBChild = muaHangHoaViewModel.Model.MSP_MuaHangHoa_ThueTTDBs[e.Index];

            System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
            DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = ((List<object>)dm_HangHoa_CollectionViewSource.Source)[0] as DM_HangHoa_ListViewModel;
            System.Collections.Generic.IList<DM_HangHoaInfo> list = dm_HangHoa_ListViewModel.Model.getListOfInfo();

            System.Windows.Data.CollectionViewSource dm_TaiKhoan_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoan_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TaiKhoan_ListViewModel = ((List<object>)dm_TaiKhoan_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            System.Collections.Generic.IList<DM_TaiKhoanInfo> listTaiKhoan = dm_TaiKhoan_ListViewModel.Model.getListOfInfo();

          
            foreach (DM_HangHoaInfo item in list)
            {
                if (item.Khoa == e.HangHoaId)
                {
                    ChiTietChild.DienGiai = item.TenViet;
                    ChiTietChild.KhoId = item.KhoNgamDinhId;
                    ChiTietChild.TKKhoId = (int)item.TKKhoId;
                    ChiTietChild.DonGia = item.DonGiaBan;
                    ChiTietChild.TKNoId = (int)item.TKKhoId;
                    ChiTietChild.TKCoId = listTaiKhoan[0].Khoa;
                    ChiTietChild.DVTId = item.DonViTinhId;

                    ThueChild.HangHoaId = item.Khoa;
                    ThueChild.DienGiai = item.TenViet;
                    ThueChild.LoaiHoaDonId = muaHangHoaViewModel.Model.LoaiHoaDonId;
                    ThueChild.ThueSuatId = item.ThueSuatId;
                    ThueChild.TKNoId = listTaiKhoan[0].Khoa;
                    ThueChild.TKCoId = ChiTietChild.TKCoId;


                    ThueNKChild.HangHoaId = item.Khoa;
                    ThueNKChild.DienGiai = item.TenViet;
                    ThueNKChild.TkNoId = listTaiKhoan[0].Khoa;
                    ThueNKChild.TkCoId = listTaiKhoan[0].Khoa;



                    TSThuaCXLChild.HangHoaId = item.Khoa;
                    TSThuaCXLChild.DienGiai = item.TenViet;
                    TSThuaCXLChild.TkNoId = listTaiKhoan[0].Khoa;
                    TSThuaCXLChild.TkCoId = listTaiKhoan[0].Khoa;

                    TSThieuCXLChild.HangHoaId = item.Khoa;
                    TSThieuCXLChild.DienGiai = item.TenViet;
                    TSThieuCXLChild.TkNoId = listTaiKhoan[0].Khoa;
                    TSThieuCXLChild.TkCoId = listTaiKhoan[0].Khoa;


                    ChiPhiMuaHangChild.HangHoaId = item.Khoa;
                    ChiPhiMuaHangChild.DienGiai = item.TenViet;
                    ChiPhiMuaHangChild.TkNoId = listTaiKhoan[0].Khoa;
                    ChiPhiMuaHangChild.TkCoId = listTaiKhoan[0].Khoa;

                    ThueTTDBChild.HangHoaId = item.Khoa;
                    ThueTTDBChild.DienGiai = item.TenViet;
                    ThueTTDBChild.TkNoId = listTaiKhoan[0].Khoa;
                    ThueTTDBChild.TkCoId = listTaiKhoan[0].Khoa;

                    break;
                }
            }
        }
        
        void TM_PhieuChi_HangHoaIdChanged(object sender, HangHoaIdChangedEventArgs e)
        {
           
            /*TM_PhieuChi_ChiTietChild ChiTietChild =  GetViewModel().Model.TM_PhieuChi_ChiTiets[e.Index];
            TM_PhieuChi_ThueChild ThueChild =  GetViewModel().Model.TM_PhieuChi_Thues[e.Index];
            
            System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
            DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = ((List<object>)dm_HangHoa_CollectionViewSource.Source)[0] as DM_HangHoa_ListViewModel;
            System.Collections.Generic.IList<DM_HangHoaInfo> list = dm_HangHoa_ListViewModel.Model.getListOfInfo();

                
            foreach (DM_HangHoaInfo item in list)
            {
                if (item.Khoa == e.HangHoaId)
                {
                    ChiTietChild.DienGiai = item.TenViet;
                    ChiTietChild.SoTien = (double) item.DonGiaBan;
                    ThueChild.DienGiai = item.TenViet;
                    ThueChild.ThueSuatId = item.ThueSuatId;    
                    break;
                }
            }
          
            */
     
        }

       
        #endregion

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.tabControl.Visibility == Visibility.Visible)
            {
                if (this.GetViewModel().CanSave)
                {
                    this.GetViewModel().SavePhieuChi();
                }
            }
            else if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
            {
                
                System.Windows.Data.CollectionViewSource msp_MuaHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaHangHoa_ViewModelSource"];
                MSP_MuaHangHoa_ViewModel muaHangHoaViewModel = ((List<object>)msp_MuaHangHoa_CollectionViewSource.Source)[0] as MSP_MuaHangHoa_ViewModel;

                if (this.GetViewModel().CanSave)
                {
                    this.GetViewModel().SavePhieuChi();
                }
                if (muaHangHoaViewModel.CanSave)
                {
                    muaHangHoaViewModel.SaveMuaHangHoa();
                }

            }
            else if (this.MuaDichVuTabControl.Visibility == Visibility.Visible)
            {
                
                System.Windows.Data.CollectionViewSource msp_MuaDichVu_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaDichVu_ViewModelSource"];
                MSP_MuaDichVu_ViewModel muaDichVuViewModel = ((List<object>)msp_MuaDichVu_CollectionViewSource.Source)[0] as MSP_MuaDichVu_ViewModel;

                if (this.GetViewModel().CanSave)
                {
                    this.GetViewModel().SavePhieuChi();
                }
                if (muaDichVuViewModel.CanSave)
                {
                    muaDichVuViewModel.SaveMuaDichVu();
                }

            }
        }


        private void AddPhieuChiDetailsItem()
        {
            GetViewModel().AddTM_PhieuChi_ChiTietItem();
        }

        private void DeletePhieuChiDetailsItem(int Index)
        {
            GetViewModel().DeleteTM_PhieuChi_ChiTietItem(Index);
        }

        private void AddMuaHangHoaDetailsItem()
        {
            GetMuaHangHoaViewModel().AddMSP_MuaHangHoa_ChiTietItem();
        }


        private void DeleteMuaHangHoaDetailsItem(int Index)
        {
            GetMuaHangHoaViewModel().DeleteMSP_MuaHangHoa_ChiTietItem(Index);
        }

        private void AddMuaDichVuDetailsItem()
        {
            GetMuaDichVuViewModel().AddMSP_MuaDichVu_ChiTietItem();
        }

        private void DeleteMuaDichVuDetailsItem(int Index)
        {
            GetMuaDichVuViewModel().DeleteMSP_MuaDichVu_ChiTietItem(Index);
        }


        void msp_MuaHangHoa_ViewModel_ChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (e.ChildListName == "MSP_MuaHangHoa_ChiTiets")
            {
                this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "MSP_MuaHangHoa_Thues")
            {
                this.MSP_MuaHangHoa_ChiPhiMuaHangGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "MSP_MuaHangHoa_ChiPhiMuaHangs")
            {
                this.MSP_MuaHangHoa_ChiPhiMuaHangGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "MSP_MuaHangHoa_TSThieuCXLs")
            {
                this.MSP_MuaHangHoa_TSThieuCXLGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "MSP_MuaHangHoa_TSThuaCXLs")
            {
                this.MSP_MuaHangHoa_TSThuaCXLGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "MSP_MuaHangHoa_ThueNKs")
            {
                this.MSP_MuaHangHoa_ThueNKGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "MSP_MuaHangHoa_ThueTTDBs")
            {
                this.MSP_MuaHangHoa_ThueTTDBGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
        }

        void MSP_MuaDichVu_ViewModel_ChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (e.ChildListName == "MSP_MuaDichVu_ChiTiets")
            {
                this.MSP_MuaDichVu_MuaDichVuChiTietGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "MSP_MuaDichVu_Thues")
            {
                this.MSP_MuaDichVu_ChiPhiMuaDichVuGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "MSP_MuaDichVu_ChiPhiMuaDichVus")
            {
                this.MSP_MuaDichVu_ChiPhiMuaDichVuGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }

        }

        void TM_PhieuChi_ViewModel_ChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (e.ChildListName == "TM_PhieuChi_ChiTiets")
            {
                this.TM_PhieuChi_PhieuChiChiTietGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "TM_PhieuChi_Thues")
            {
                this.TM_PhieuChi_ThueChiTietGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }

        }
        
    }
}
